const LOG_NONE = 0
const LOG_INFO = 1
const LOG_DEBUG = 2
const LOG_WARN = 4
const LOG_ERROR = 8

const logLevel = LOG_INFO | LOG_DEBUG | LOG_WARN | LOG_ERROR

export function logInfo(...args) {
  // logLevel= 1,2,3,4
  let timeStr = getTimeString("info");
  if ((logLevel & LOG_INFO) == LOG_INFO) {
    // 1,2,3,4
    console.info(timeStr, ...args)
  }
}

export function logDebug(...args) {
  // logLevel= 2,3,4
  let timeStr = getTimeString("debug");
  if ((logLevel & LOG_DEBUG) == LOG_DEBUG) {
    console.debug(timeStr, ...args)
  }
}

export function logWarn(...args) {
  // logLevel= 3,4
  let timeStr = getTimeString("warn");
  if ((logLevel & LOG_WARN) == LOG_WARN) {
    console.warn(timeStr, ...args)
  }
}

export function logError(...args) {
  // logLevel= 4
  let timeStr = getTimeString("error");
  if ((logLevel & LOG_ERROR) == LOG_ERROR) {
    console.error(timeStr, ...args)
  }
}

// var getStackTrace = function() {
//   var obj = {};
//   Error.captureStackTrace(obj, getStackTrace);
//   return obj.stack;
// };

// function foo(){
//     return getStackTrace();
// }

// alert("Stack:" + foo());
// alert("Location:" + foo().split(/\n+/)[1].replace(/(^\s+|\s+$)/,""));


// Object.defineProperty(global, '__stack', {
//   get: function () {
//     var path = require('path');
//     var stackReg = /at\s+(.*)\s+\((.*):(\d*):(\d*)\)/i;
//     var stackReg2 = /at\s+()(.*):(\d*):(\d*)/i;
//     var stacklist = (new Error()).stack.split('\n').slice(3);
//     var s = stacklist[0];
//     var sp = stackReg.exec(s) || stackReg2.exec(s);
//     var data = {};
//     if (sp && sp.length === 5) {
//       data.method = sp[1];
//       data.path = sp[2];
//       data.line = sp[3];
//       data.pos = sp[4];
//       data.file = path.basename(data.path);
//     }

//     return data;
//   }
// });

// //行号 
// Object.defineProperty(global, '__line', {
//   get: function () {
//     return __stack['line'];
//   }
// });
// //文件名
// Object.defineProperty(global, '__file', {
//   get: function () {
//     return __stack['file'];
//   }
// });
// //方法名
// Object.defineProperty(global, '__func', {
//   get: function () {
//     return __stack['method'];
//   }
// });

function formatFunction(date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  const o = {
    "M+": date.getMonth() + 1, // 月份
    "d+": date.getDate(), // 日
    "h+": date.getHours(), // 小时
    "m+": date.getMinutes(), // 分
    "s+": date.getSeconds(), // 秒
    "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
    S: date.getMilliseconds() // 毫秒
  };
  for (let k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1
          ? o[k]
          : ("00" + o[k]).substr(("" + o[k]).length)
      );
    }
  }
  return fmt;
}

function getTimeString(type) {
  return formatFunction(new Date(), 'yyyy-MM-dd hh:mm:ss') + " [" + type + "]>>";
}

function getTimer() {
  return formatFunction(new Date(), 'yyyy-MM-dd hh:mm:ss') + " >>";
}

/* 
js调试组件 
*/
(function () {
  var logger = function (level, object, viewType) {
    this.level = level;
    this.object = object;
    this.viewType = viewType;
  }
  logger.LEVEL_DEBUG = 0;
  logger.LEVEL_INFO = 1;
  logger.LEVEL_WARN = 2;
  logger.LEVEL_ERROR = 3;
  logger.LEVEL_FATAL = 4;
  logger.VIEW_TYPE_ALERT = 0;
  logger.VIEW_TYPE_APPEND = 1;
  logger.prototype = {
    setLevel: function (level) {
      this.level = level;
    },
    setObject: function (o) {
      if (typeof o == 'string') {
        this.object = document.getElementById(o);
      } else {
        this.object = o;
      }
    },
    setViewType: function (type) {
      this.viewType = type;
    },
    log: function (s) {
      this.message(100, s);
    },
    debug: function (s) {
      this.message(logger.LEVEL_DEBUG, s);
    },
    info: function (s) {
      this.message(logger.LEVEL_INFO, s);
    },
    warn: function (s) {
      this.message(logger.LEVEL_WARN, s);
    },
    error: function (s) {
      this.message(logger.LEVEL_ERROR, s);
    },
    fatal: function (s) {
      this.message(logger.LEVEL_FATAL, s);
    },
    message: function (level, s) {
      if (level >= this.level) {
        if (this.object != null) {
          this.object.innerHTML = s;
        } else if (this.viewType == logger.VIEW_TYPE_ALERT) {
          alert(s);
        } else {
          document.body.appendChild(document.createTextNode(getTimer()));
          document.body.appendChild(document.createTextNode(s));
          document.body.appendChild(document.createElement("br"));
        }
      }
    }
  };
  if (typeof window.log == 'undefined' || window.log == null)
    window.log = new logger(logger.LEVEL_DEBUG, null, logger.VIEW_TYPE_APPEND);
})();
